package ru.mcfr.oxygen.framework.operations.table;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: ws
 * Date: 29.03.11
 * Time: 17:24
 * To change this template use File | Settings | File Templates.
 */
public class Switcher {
    private Map<Integer, String> initPos;

    {
        initPos = new HashMap<Integer, String>();
    }

    private Set<String> currentPos = new HashSet<String>();

    private void init(String[] positions) {
        for (int i = 0; i < positions.length; i++)
            initPos.put(i, positions[i]);
    }

    public Switcher(String[] positions) {
        init(positions);
    }

    public Switcher() {
        init(new String[]{"жирный", "курсив", "подчеркнутый", "зачеркнутый"});
    }

    public void initPositionBy(String arg, String separator) {
        for (String ar : arg.split("\\" + separator))
            if (currentPos.contains(ar))
                currentPos.remove(ar);
            else
                currentPos.add(ar);
    }

    public void initPositionBy_(String arg) {
        initPositionBy(arg, "_");
    }

    public String toggleValueBy(String val, String seporator) {
        String result = "";

        if (currentPos.contains(val))
            currentPos.remove(val);
        else
            currentPos.add(val);

        for (int key : initPos.keySet())
            if (currentPos.contains(initPos.get(key)))
                result = result + initPos.get(key) + seporator;

        if (result.lastIndexOf(seporator) == result.length() - 1)
            result = result.substring(0, result.lastIndexOf(seporator));

        return result;
    }

    public String toggleByValue_(String val) {
        return toggleValueBy(val, "_");
    }
}


